package dm.com.dmexamples.image_chooser;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

import java.util.List;

import dm.com.dmexamples.R;

public class ListImageDirPopupWindow extends BasePopupWindowForListView<ImageFloder> {
    private ListView mListDir;
    private BeanAdapter beanAdapter;
    private String currentDir;

    public ListImageDirPopupWindow(int width, int height, List<ImageFloder> datas, View convertView) {
        super(convertView, width, height, true, datas);
    }

    @Override
    public void initViews() {
        mListDir = (ListView) findViewById(R.id.id_list_dir);
        beanAdapter = new BeanAdapter<ImageFloder>(context, R.layout.img_chooser_list_dir_item) {
            @Override
            public void bindView(View itemV, int position, ImageFloder item) {
                TextView textViewName = (TextView) itemV.findViewById(R.id.id_dir_item_name);
                TextView textViewCount = (TextView) itemV.findViewById(R.id.id_dir_item_count);
                textViewName.setText(item.getName());
                textViewCount.setText(item.getCount() + "张");
                ImageView imageView = (ImageView) itemV.findViewById(R.id.id_dir_item_image);
                ImageView imageViewChecked = (ImageView) itemV.findViewById(R.id.id_dir_item_checked);
                Glide.with(mContext).load("file://" + item.getFirstImagePath()).crossFade().into(imageView);
                if (currentDir != null && currentDir.equalsIgnoreCase(item.getDir())) {
                    imageViewChecked.setVisibility(View.VISIBLE);
                } else {
                    imageViewChecked.setVisibility(View.INVISIBLE);
                }
            }
        };
        mListDir.setAdapter(beanAdapter);
    }

    public interface OnImageDirSelected {
        void selected(ImageFloder floder);
    }

    private OnImageDirSelected mImageDirSelected;

    public void setOnImageDirSelected(OnImageDirSelected mImageDirSelected) {
        this.mImageDirSelected = mImageDirSelected;
    }

    @Override
    public void initEvents() {
        mListDir.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (mImageDirSelected != null) {
                    mImageDirSelected.selected(mDatas.get(position));
                }
            }
        });
    }

    @Override
    public void init() {
        beanAdapter.addAll(mDatas);
    }

    @Override
    protected void beforeInitWeNeedSomeParams(Object... params) {

    }

    /**
     * 设置选中
     *
     * @param dir
     */
    public void setDirSelected(String dir) {
        this.currentDir = dir;
        beanAdapter.notifyDataSetChanged();
    }
}
